package com.example.demo.util;


import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.ast.NodeVisitor;
import com.vladsch.flexmark.ast.Text;
import com.vladsch.flexmark.ast.VisitHandler;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.options.MutableDataSet;



public class RemoveMarkdownTag {
    public String removeMarkdownTags(String markdownText) {
        MutableDataSet options = new MutableDataSet();

        Parser parser = Parser.builder(options).build();
        Node document = parser.parse(markdownText);

        StringBuilder sb = new StringBuilder();
        NodeVisitor visitor = new NodeVisitor(
                new VisitHandler<>(Text.class, text -> {
                    // Collect the text from text nodes
                    sb.append(text.getChars().normalizeEOL());
                })
        );
        visitor.visit(document);

        return sb.toString();
    }
}
